/*
 * Sprite.cpp
 *
 *  Created on: 14 jan 2010
 *      Author: Andreas
 */

#include "Sprite.h"

#include "Image.h"
#include <math.h>
#include <iostream>
#include "ScreenManager.h"

using namespace std;

Sprite::Sprite() : Image("") {
	xpos = 0;
	ypos = 0;
	dx=0;
	dy=0;
	angle = 0;
	animation = NULL;
	scaleX = 1.0;
	scaleY = 1.0;
	fixedToScr = false;
	collisionCircle = false;
}

Sprite::Sprite(string file) : Image(file) {
	xpos = 0;
	ypos = 0;
	dx=0;
	dy=0;
	angle = 0;
	animation = NULL;
	scaleX = 1.0;
	scaleY = 1.0;
	fixedToScr = false;
	collisionCircle = false;
}

Sprite::~Sprite() {
	if (animation != NULL) delete animation;
}

void Sprite::draw() {

	Image::draw(animation);

	if (collisionCircle) {
		glLoadIdentity();
		glTranslatef(ScreenManager::screenXpos(xpos,fixedToScr), ScreenManager::screenYpos(ypos,fixedToScr), 0.0);
		glColor3f(1.0f, 1.0f, 1.0f);
		glBegin(GL_LINE_LOOP);

		for (int i=0; i<360; i++) {
			float degInRad = i * (3.13159/180.0);
			glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
		}

		glEnd();
	}

}

void Sprite::loadAnimation(int rows, int cols, int frametime) {
	animation = new SpriteAnimation(rows, cols, frametime, this);
}

void Sprite::move(float dx, float dy) {
	setSpeed(dx,dy);
	updatePos();
}


void Sprite::setSpeed(float dx, float dy) {
	this->dx= dx;
	this->dy= dy;
}

void Sprite::updatePos(){
	xpos +=dx;
	ypos +=dy;
	/*if (xpos > 1680) {
		xpos = 0;
	}
	else if (xpos < 0){
		xpos = 1680;
	}
	if (ypos > 1050) {
		ypos = 0;
	}
	else if (ypos < 0){
		ypos = 1050;
	}*/
}

void Sprite::updateSpeed(double a, double s){
	rotate(a);
	if (angle>360){
		angle=0;
	}
	dx += (float) (s*cos(PI*angle/180));
	dy -= (float) (s*sin(PI*angle/180));
}





SpriteAnimation *Sprite::getAnimation(){
	return animation;
}



